package com.sweet.eventbus;

public class EventBusDemo {

  public static void main(String[] args) throws InterruptedException {
    EventBus eventBus = EventBus.instance();

    Demo1 demo1 = new Demo1(eventBus);
    Demo2 demo2 = new Demo2(eventBus);

    demo2.sayHello(Demo1.class.getName(), "我是demo2, hello");
    demo1.sayHello(Demo2.class.getName(), "我是demo1, hello");

    System.out.println("end");
    Thread.sleep(200L);
  }
}

class Demo1 {
  private final EventBus eventBus;

  public Demo1(EventBus eventBus) {
    this.eventBus = eventBus;
    address();
  }

  public void sayHello(String address, String msg) {
    eventBus.send(address, msg);
  }

  public void address() {
    eventBus.consumer(Demo1.class.getName(), res -> {
      System.out.println(Demo1.class.getName() + ": " + res.body());
    });
  }
}


class Demo2 {

  private final EventBus eventBus;

  public Demo2(EventBus eventBus) {
    this.eventBus = eventBus;
    address();
  }

  public void sayHello(String address, String msg) {
    eventBus.send(address, msg);
  }

  public void address() {
    eventBus.consumer(Demo2.class.getName(), res -> {
      System.out.println(Demo2.class.getName() + ": " + res.body());
    });
  }
}